package com.jacklin.controller;

import com.jacklin.VO.ProductInfoVO;
import com.jacklin.VO.ProductVO;
import com.jacklin.VO.ResultVO;
import com.jacklin.entity.ProductCategory;
import com.jacklin.entity.ProductInfo;
import com.jacklin.service.CategoryService;
import com.jacklin.service.ProductInfoService;
import com.jacklin.utils.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 买家商品控制层
 * @Author:JackLin
 * @Create 2019/3/26 22:05
 */
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private CategoryService categoryService;


    @GetMapping("/list")
    @Cacheable(cacheNames = "product",key = "123")      //将信息保存在redis
    public ResultVO productInfoList(){


        //1.查询上架商品
        List<ProductInfo> productInfoList = productInfoService.findUpAll();

        //2.查询类目列表(一次性查询)
        /**
         *     List<Integer> CategoryTypeList = new ArrayList<>();
         *        传统方法查询实现
         *      for(ProductInfo productInfo : productInfoList){
         *          CategoryTypeList.add(productInfo.getCategoryType());
         *      }
         */

        //使用lambda表达式实现(java 8特性)
        List<Integer> categoryTypeList = productInfoList.stream()
                .map(e -> e.getCategoryType())
                .collect(Collectors.toList());
        List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryTypeList);

        //3、数据拼装(就是把数据拼装成返回给前端的格式)
        List<ProductVO> productVOList = new ArrayList<>();
        for (ProductCategory productCategory:productCategoryList) {
            ProductVO productVO = new ProductVO();  //最外层数据
            productVO.setCategoryName(productCategory.getCategoryName());
            productVO.setCategoryType(productCategory.getCategoryType());


            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for (ProductInfo productInfo : productInfoList) {
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())) {
                    ProductInfoVO productInfoVO = new ProductInfoVO();  //商品数据
//                    productInfoVO.setProductId();
//                    productInfoVO.setCategoryType();
//                    productInfoVO.setGetProductDescription();
//                    productInfoVO.setProductIcon();
//                    productInfoVO.setProductPrice();
                    BeanUtils.copyProperties(productInfo, productInfoVO);    //把productInfo copy到productInfoVO里
                    productInfoVOList.add(productInfoVO);

                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }

        return ResultVOUtil.success(productVOList);

    }
}
